package com.zxy.health.service;

import com.zxy.health.dao.StudentDao;
import com.zxy.health.mapper.HealthCodeMapper;
import com.zxy.health.mapper.InputSchoolMapper;
import com.zxy.health.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StudentService {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    HealthCodeMapper healthCodeMapper;
    @Autowired
    InputSchoolMapper inputSchoolMapper;

    public List<StudentDao> getStudentList(){
        return studentMapper.getStudentList();
    }

    public StudentDao getStudentByLogin(String name,String password){
        StudentDao studentDao = studentMapper.getStudent(name,password);
        exchangeHealthcode(studentDao.getSchoolid());
        return studentDao;
    }

    public StudentDao getStudentBySchoolId(String schoolId){
        if (studentMapper.getStudentBySchoolId(schoolId).size()!=0){
            exchangeHealthcode(schoolId);
            return studentMapper.getStudentBySchoolId(schoolId).get(0);
        }else {
            return null;
        }
    }

    public List<StudentDao> getStudentByClassName(String classname){
        if (studentMapper.getStudentByClassname(classname).size()!=0){
            return studentMapper.getStudentByClassname(classname);
        }else {
            return null;
        }

    }

    public String insertStudent(StudentDao studentDao){
        if (studentMapper.getStudentBySchoolId(studentDao.getSchoolid()).size()!=0){
            return "添加错误，信息重复";
        }else {
            studentMapper.insertStudent(studentDao);
            return "success";
        }
    }

    public void exchangeHealthcode(String schoolid){
        if (inputSchoolMapper.getInputSchoolRecordBySchoolId(schoolid).size()!=0&&healthCodeMapper.getHealthCodeBySchoolId(schoolid).size()!=0){
            //若为出门并且一天内未上报 变紫，若当前的时间超过上次上报的时间一周 变紫
            if ((inputSchoolMapper.getInputSchoolRecordBySchoolId(schoolid).get(0).getState()==0&&
                    new Date().getTime()-healthCodeMapper.getHealthCodeBySchoolId(schoolid).get(0).getReporttime()>86400000
            )||
                    new Date().getTime()-healthCodeMapper.getHealthCodeBySchoolId(schoolid).get(0).getReporttime()>604800000){
                studentMapper.updateStudentHealthcode(schoolid,4);
            }
        }

    }

}
